package tcc.dao;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Collection;
import java.util.Date;
import java.util.HashSet;

import tcc.model.Usuario;
import tcc.util.DBSingleton;

public class UsuarioDAO implements DaoBaseInterface<Usuario> {

	public void salvar(Usuario u) {
		try {
			PreparedStatement ps = DBSingleton
					.prepareStatement(
							"insert into usuario (nome, email, nick, senhaMD5, data_nasc, sexo, id_facebook, id_endereco, id_foto) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)",
							Statement.RETURN_GENERATED_KEYS);

			ps.setString(1, u.getNome());
			ps.setString(2, u.getEmail());
			ps.setString(3, u.getNick());
			ps.setString(4, u.getSenhaMD5());

			java.sql.Date DateSql = new java.sql.Date(u.getData_nasc()
					.getTime());
			ps.setDate(5, DateSql);

			ps.setBoolean(6, u.isSexo());
			ps.setString(7, u.getIdFacebook());
			ps.setLong(8, u.getIdEndereco());
			ps.setLong(9, u.getIdFoto());

			ps.executeUpdate();

			ResultSet ids = ps.getGeneratedKeys();
			ids.first();

			u.setId(ids.getInt(1));
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	@Override
	public void excluir(Usuario u) {
		try {
			PreparedStatement ps = DBSingleton
					.prepareStatement("delete from usuario where id_usuario=?");

			ps.setLong(1, u.getId());
			ps.executeUpdate();
		} catch (SQLException e) {
			System.out.println("erro no metodo UsuarioDAO.excluir");
			e.printStackTrace();
			System.out.println("Error Message: " + e.getMessage());
		}
	}

	@Override
	public void atualizar(Usuario u) {
		try {
			PreparedStatement ps = DBSingleton
					.prepareStatement("update usuario set nome=?, email=?, nick=?, senhaMD5=?, data_nasc=?, sexo=?, id_facebook=?, id_endereco=?, id_foto=? where id_usuario=?");

			ps.setString(1, u.getNome());
			ps.setString(2, u.getEmail());
			ps.setString(3, u.getNick());
			ps.setString(4, u.getSenhaMD5());

			java.sql.Date DateSql = new java.sql.Date(u.getData_nasc()
					.getTime());
			ps.setDate(5, DateSql);

			ps.setBoolean(6, u.isSexo());
			ps.setString(7, u.getIdFacebook());
			ps.setLong(8, u.getIdEndereco());
			ps.setLong(9, u.getIdFoto());
			ps.setLong(10, u.getId());

			ps.executeUpdate();
			
			System.out.println("nome: "+u.getNome());

		} catch (SQLException e) {
			System.out.println("erro no metodo UsuarioDAO.atualizar");
			e.printStackTrace();
			System.out.println("Error Message: " + e.getMessage());
		}

	}

	@Override
	public Collection<Usuario> recuperarTodos() {
		return null;
	}

	@Override
	public Collection<Usuario> recuperarTodosComOrdenacao(String orderBy) {
		return null;
	}

	public Usuario recuperarPorId(Long id) {
		Usuario u = new Usuario();
		try {
			Statement statement = DBSingleton.getStatement();
			statement.execute("select * from usuario where id_usuario=" + id);
			ResultSet rs = statement.getResultSet();

			// coloca o cursor na primeira linha dos resultados.
			boolean resultadoValido = rs.first();

			if (resultadoValido == false) {
				// re não existe nenhum resultado então retorna null sem
				// apresentar erro.
				return null;
			}
			u.setId(rs.getInt("id_usuario"));
			u.setNome((String) rs.getObject("nome"));
			u.setEmail((String) (rs.getObject("email")));
			u.setData_nasc((Date) rs.getObject("data_nasc"));
			u.setNick((String) rs.getObject("nick"));
			u.setSenhaMD5((String) rs.getObject("senhaMD5"));
			u.setIdEndereco(rs.getInt("id_endereco"));
			u.setIdFacebook(rs.getString("id_facebook"));
			u.setIdFoto(rs.getInt("id_foto"));

		} catch (SQLException e) {
			System.out.println("erro no metodo UsuarioDAO.recuperarPorId");
			e.printStackTrace();
			System.out.println("Error Message: " + e.getMessage());
			return null;
		}

		return u;
	}

	public Usuario recuperarPorEmail(String email) {
		Usuario u = new Usuario();
		try {
			// PreparedStatement ps = DBSingleton
			// .prepareStatement("SELECT * FROM \"MERCADORIA\" WHERE \"ID\" = ?");

			PreparedStatement ps = DBSingleton
					.prepareStatement("select * from usuario where email= ?");

			ps.setString(1, email);
			ps.execute();

			ResultSet rs = ps.getResultSet();
			boolean resultadoValido = rs.first();

			if (resultadoValido == false) {
				return null;// não existe nenhum resultado então retorna null
							// sem dar erro.
			}

			u.setId(rs.getInt("id_usuario"));
			u.setNome((String) rs.getObject("nome"));
			u.setEmail((String) (rs.getObject("email")));
			u.setData_nasc((Date) rs.getObject("data_nasc"));
			u.setNick((String) rs.getObject("nick"));
			u.setSenhaMD5((String) rs.getObject("senhaMD5"));
			u.setIdEndereco(rs.getInt("id_endereco"));
			u.setIdFacebook(rs.getString("id_facebook"));
			u.setIdFoto(rs.getInt("id_foto"));

			ps.close();

		} catch (SQLException e) {
			System.out.println("erro no metodo UsuarioDAO.recuperarPorEmail");
			e.printStackTrace();
			System.out.println("Error Message: " + e.getMessage());
			return null;
		}

		return u;
	}

}
